/*     */ package com.alipay.api;
/*     */ 
/*     */ import com.alipay.api.internal.parser.json.ObjectJsonParser;
/*     */ import com.alipay.api.internal.util.AlipayHashMap;
/*     */ import com.alipay.api.internal.util.AlipayLogger;
/*     */ import com.alipay.api.internal.util.AlipaySignature;
/*     */ import com.alipay.api.internal.util.RequestParametersHolder;
/*     */ import com.alipay.api.internal.util.StreamUtil;
/*     */ import com.alipay.api.internal.util.StringUtils;
/*     */ import com.alipay.api.internal.util.json.JSONWriter;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.OutputStream;
/*     */ import java.io.StringWriter;
/*     */ import java.net.HttpURLConnection;
/*     */ import java.net.URL;
/*     */ import java.net.URLEncoder;
/*     */ import java.security.SecureRandom;
/*     */ import java.security.Security;
/*     */ import java.security.cert.CertificateException;
/*     */ import java.security.cert.X509Certificate;
/*     */ import java.text.DateFormat;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.TimeZone;
/*     */ import java.util.TreeMap;
/*     */ import javax.net.ssl.HostnameVerifier;
/*     */ import javax.net.ssl.HttpsURLConnection;
/*     */ import javax.net.ssl.SSLContext;
/*     */ import javax.net.ssl.SSLSession;
/*     */ import javax.net.ssl.TrustManager;
/*     */ import javax.net.ssl.X509TrustManager;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class AlipayMobilePublicMultiMediaClient
/*     */   implements AlipayClient
/*     */ {
/*     */   private static final String DEFAULT_CHARSET = "UTF-8";
/*     */   private static final String METHOD_POST = "POST";
/*     */   private static final String METHOD_GET = "GET";
/*     */   private String serverUrl;
/*     */   private String appId;
/*     */   private String privateKey;
/*     */   private String prodCode;
/*  58 */   private String format = "json";
/*  59 */   private String sign_type = "RSA";
/*     */   
/*     */   private String charset;
/*     */   
/*  63 */   private int connectTimeout = 3000;
/*  64 */   private int readTimeout = 15000;
/*     */   
/*     */   private static class DefaultTrustManager implements X509TrustManager {
/*     */     public X509Certificate[] getAcceptedIssuers() {
/*  68 */       return null;
/*     */     }
/*     */ 
/*     */     
/*     */     private DefaultTrustManager() {}
/*     */ 
/*     */     
/*     */     public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
/*     */     
/*     */     public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
/*     */   }
/*     */   
/*     */   static {
/*  81 */     Security.setProperty("jdk.certpath.disabledAlgorithms", "");
/*     */   }
/*     */   
/*     */   public AlipayMobilePublicMultiMediaClient(String serverUrl, String appId, String privateKey) {
/*  85 */     this.serverUrl = serverUrl;
/*  86 */     this.appId = appId;
/*  87 */     this.privateKey = privateKey;
/*     */   }
/*     */ 
/*     */   
/*     */   public AlipayMobilePublicMultiMediaClient(String serverUrl, String appId, String privateKey, String format) {
/*  92 */     this(serverUrl, appId, privateKey);
/*  93 */     this.format = format;
/*     */   }
/*     */ 
/*     */   
/*     */   public AlipayMobilePublicMultiMediaClient(String serverUrl, String appId, String privateKey, String format, String charset) {
/*  98 */     this(serverUrl, appId, privateKey);
/*  99 */     this.format = format;
/* 100 */     this.charset = charset;
/*     */   }
/*     */ 
/*     */   
/*     */   public AlipayMobilePublicMultiMediaClient(String serverUrl, String appId, String privateKey, String format, String charset, String signType) {
/* 105 */     this(serverUrl, appId, privateKey, format, charset);
/*     */     
/* 107 */     this.sign_type = signType;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public <T extends AlipayResponse> T execute(AlipayRequest<T> request) throws AlipayApiException {
/* 114 */     return execute(request, null);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public <T extends AlipayResponse> T execute(AlipayRequest<T> request, String accessToken) throws AlipayApiException {
/* 123 */     return execute(request, accessToken, null);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public <T extends AlipayResponse> T execute(AlipayRequest<T> request, String accessToken, String appAuthToken) throws AlipayApiException {
/* 132 */     return _execute(request, accessToken, appAuthToken);
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   private <T extends AlipayResponse> T _execute(AlipayRequest<T> request, String authToken, String appAuthToken) throws AlipayApiException {
/* 138 */     return doGet(request, appAuthToken);
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public <T extends AlipayResponse> T doGet(AlipayRequest<T> request, String appAuthToken) throws AlipayApiException {
/* 144 */     if (StringUtils.isEmpty(this.charset)) {
/* 145 */       this.charset = "UTF-8";
/*     */     }
/*     */     
/* 148 */     RequestParametersHolder requestHolder = new RequestParametersHolder();
/* 149 */     AlipayHashMap appParams = new AlipayHashMap(request.getTextParams());
/*     */ 
/*     */     
/*     */     try {
/* 153 */       if (request.getClass().getMethod("getBizContent", new Class[0]) != null && 
/* 154 */         StringUtils.isEmpty((String)appParams.get("biz_content")) && request
/* 155 */         .getBizModel() != null) {
/* 156 */         appParams.put("biz_content", (new JSONWriter())
/* 157 */             .write(request.getBizModel(), true));
/*     */       }
/* 159 */     } catch (NoSuchMethodException noSuchMethodException) {
/*     */     
/* 161 */     } catch (SecurityException e) {
/* 162 */       AlipayLogger.logBizError(e);
/*     */     } 
/*     */     
/* 165 */     if (!StringUtils.isEmpty(appAuthToken)) {
/* 166 */       appParams.put("app_auth_token", appAuthToken);
/*     */     }
/*     */     
/* 169 */     requestHolder.setApplicationParams(appParams);
/*     */     
/* 171 */     AlipayHashMap protocalMustParams = new AlipayHashMap();
/* 172 */     protocalMustParams.put("method", request.getApiMethodName());
/* 173 */     protocalMustParams.put("version", request.getApiVersion());
/* 174 */     protocalMustParams.put("app_id", this.appId);
/* 175 */     protocalMustParams.put("sign_type", this.sign_type);
/* 176 */     protocalMustParams.put("charset", this.charset);
/*     */     
/* 178 */     Long timestamp = Long.valueOf(System.currentTimeMillis());
/* 179 */     DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/* 180 */     df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
/* 181 */     protocalMustParams.put("timestamp", df.format(new Date(timestamp.longValue())));
/* 182 */     protocalMustParams.put("format", this.format);
/* 183 */     requestHolder.setProtocalMustParams(protocalMustParams);
/*     */     
/* 185 */     if ("RSA".equals(this.sign_type)) {
/*     */       
/* 187 */       String signContent = AlipaySignature.getSignatureContent(requestHolder);
/*     */       
/* 189 */       protocalMustParams.put("sign", 
/* 190 */           AlipaySignature.rsaSign(signContent, this.privateKey, this.charset));
/*     */     }
/* 192 */     else if ("RSA2".equals(this.sign_type)) {
/*     */       
/* 194 */       String signContent = AlipaySignature.getSignatureContent(requestHolder);
/*     */       
/* 196 */       protocalMustParams.put("sign", 
/* 197 */           AlipaySignature.rsa256Sign(signContent, this.privateKey, this.charset));
/*     */     } else {
/*     */       
/* 200 */       protocalMustParams.put("sign", "");
/*     */     } 
/*     */     
/* 203 */     AlipayMobilePublicMultiMediaDownloadResponse rsp = null;
/*     */     
/*     */     try {
/* 206 */       if (request instanceof AlipayMobilePublicMultiMediaDownloadRequest)
/*     */       {
/* 208 */         OutputStream outputStream = ((AlipayMobilePublicMultiMediaDownloadRequest)request).getOutputStream();
/* 209 */         rsp = doGet(this.serverUrl, requestHolder, this.charset, this.connectTimeout, this.readTimeout, outputStream);
/*     */       }
/*     */     
/* 212 */     } catch (IOException e) {
/* 213 */       throw new AlipayApiException(e);
/*     */     } 
/* 215 */     return (T)rsp;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static AlipayMobilePublicMultiMediaDownloadResponse doGet(String url, RequestParametersHolder requestHolder, String charset, int connectTimeout, int readTimeout, OutputStream output) throws AlipayApiException, IOException {
/* 237 */     HttpURLConnection conn = null;
/* 238 */     AlipayMobilePublicMultiMediaDownloadResponse response = null;
/*     */ 
/*     */     
/*     */     try {
/* 242 */       Map<String, String> params = new TreeMap<String, String>();
/* 243 */       AlipayHashMap appParams = requestHolder.getApplicationParams();
/* 244 */       if (appParams != null && appParams.size() > 0) {
/* 245 */         params.putAll((Map<? extends String, ? extends String>)appParams);
/*     */       }
/* 247 */       AlipayHashMap protocalMustParams = requestHolder.getProtocalMustParams();
/* 248 */       if (protocalMustParams != null && protocalMustParams.size() > 0) {
/* 249 */         params.putAll((Map<? extends String, ? extends String>)protocalMustParams);
/*     */       }
/* 251 */       AlipayHashMap protocalOptParams = requestHolder.getProtocalOptParams();
/* 252 */       if (protocalOptParams != null && protocalOptParams.size() > 0) {
/* 253 */         params.putAll((Map<? extends String, ? extends String>)protocalOptParams);
/*     */       }
/*     */       
/* 256 */       String ctype = "application/x-www-form-urlencoded;charset=" + charset;
/* 257 */       String query = buildQuery(params, charset);
/*     */       try {
/* 259 */         conn = getConnection(buildGetUrl(url, query), "GET", ctype);
/* 260 */         conn.setConnectTimeout(connectTimeout);
/* 261 */         conn.setReadTimeout(readTimeout);
/* 262 */       } catch (IOException e) {
/* 263 */         Map<String, String> map = getParamsFromUrl(url);
/* 264 */         AlipayLogger.logCommError(e, url, map.get("app_key"), map.get("method"), params);
/* 265 */         throw e;
/*     */       } 
/*     */       
/*     */       try {
/* 269 */         if (conn.getResponseCode() == 200) {
/* 270 */           if (conn.getContentType() != null && conn
/* 271 */             .getContentType().toLowerCase().contains("text/plain")) {
/* 272 */             String rsp = getResponseAsString(conn);
/* 273 */             ObjectJsonParser<AlipayMobilePublicMultiMediaDownloadResponse> parser = new ObjectJsonParser(AlipayMobilePublicMultiMediaDownloadResponse.class);
/*     */             
/* 275 */             response = (AlipayMobilePublicMultiMediaDownloadResponse)parser.parse(rsp);
/* 276 */             response.setBody(rsp);
/* 277 */             response.setParams((Map<String, String>)appParams);
/* 278 */             return response;
/*     */           } 
/*     */           
/* 281 */           StreamUtil.io(conn.getInputStream(), output);
/* 282 */           response = new AlipayMobilePublicMultiMediaDownloadResponse();
/* 283 */           response.setCode("200");
/* 284 */           response.setMsg("成功");
/* 285 */           response.setBody("{\"alipay_mobile_public_multimedia_download_response\":{\"code\":200,\"msg\":\"成功\"}}");
/*     */           
/* 287 */           response.setParams((Map<String, String>)appParams);
/*     */         } else {
/* 289 */           response = new AlipayMobilePublicMultiMediaDownloadResponse();
/* 290 */           response.setCode(String.valueOf(conn.getResponseCode()));
/* 291 */           response.setMsg(conn.getResponseMessage());
/* 292 */           response.setParams((Map<String, String>)appParams);
/*     */         } 
/* 294 */       } catch (IOException e) {
/* 295 */         Map<String, String> map = getParamsFromUrl(url);
/* 296 */         AlipayLogger.logCommError(e, conn, map.get("app_key"), map.get("method"), params);
/* 297 */         throw e;
/*     */       } 
/*     */     } finally {
/*     */       
/* 301 */       if (conn != null) {
/* 302 */         conn.disconnect();
/*     */       }
/* 304 */       if (output != null) {
/* 305 */         output.close();
/*     */       }
/*     */     } 
/* 308 */     return response;
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   private static HttpURLConnection getConnection(URL url, String method, String ctype) throws IOException {
/* 314 */     HttpURLConnection conn = null;
/* 315 */     if ("https".equals(url.getProtocol())) {
/* 316 */       SSLContext ctx = null;
/*     */       try {
/* 318 */         ctx = SSLContext.getInstance("TLS");
/* 319 */         ctx.init(new javax.net.ssl.KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
/*     */       }
/* 321 */       catch (Exception e) {
/* 322 */         throw new IOException(e);
/*     */       } 
/* 324 */       HttpsURLConnection connHttps = (HttpsURLConnection)url.openConnection();
/* 325 */       connHttps.setSSLSocketFactory(ctx.getSocketFactory());
/* 326 */       connHttps.setHostnameVerifier(new HostnameVerifier() {
/*     */             public boolean verify(String hostname, SSLSession session) {
/* 328 */               return false;
/*     */             }
/*     */           });
/* 331 */       conn = connHttps;
/*     */     } else {
/* 333 */       conn = (HttpURLConnection)url.openConnection();
/*     */     } 
/*     */     
/* 336 */     conn.setRequestMethod(method);
/* 337 */     conn.setDoInput(true);
/* 338 */     conn.setDoOutput(true);
/* 339 */     conn.setRequestProperty("Accept", "text/xml,text/javascript,text/html");
/* 340 */     conn.setRequestProperty("User-Agent", "aop-sdk-java");
/* 341 */     conn.setRequestProperty("Content-Type", ctype);
/* 342 */     return conn;
/*     */   }
/*     */   
/*     */   protected static String getResponseAsString(HttpURLConnection conn) throws IOException {
/* 346 */     String charset = getResponseCharset(conn.getContentType());
/* 347 */     InputStream es = conn.getErrorStream();
/* 348 */     if (es == null) {
/* 349 */       return getStreamAsString(conn.getInputStream(), charset);
/*     */     }
/* 351 */     String msg = getStreamAsString(es, charset);
/* 352 */     if (StringUtils.isEmpty(msg)) {
/* 353 */       throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
/*     */     }
/* 355 */     throw new IOException(msg);
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   private static String getStreamAsString(InputStream stream, String charset) throws IOException {
/*     */     try {
/* 362 */       BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
/* 363 */       StringWriter writer = new StringWriter();
/*     */       
/* 365 */       char[] chars = new char[256];
/* 366 */       int count = 0;
/* 367 */       while ((count = reader.read(chars)) > 0) {
/* 368 */         writer.write(chars, 0, count);
/*     */       }
/*     */       
/* 371 */       return writer.toString();
/*     */     } finally {
/* 373 */       if (stream != null) {
/* 374 */         stream.close();
/*     */       }
/*     */     } 
/*     */   }
/*     */   
/*     */   private static String getResponseCharset(String ctype) {
/* 380 */     String charset = "UTF-8";
/*     */     
/* 382 */     if (!StringUtils.isEmpty(ctype)) {
/* 383 */       String[] params = ctype.split(";");
/* 384 */       for (String param : params) {
/* 385 */         param = param.trim();
/* 386 */         if (param.startsWith("charset")) {
/* 387 */           String[] pair = param.split("=", 2);
/* 388 */           if (pair.length == 2 && 
/* 389 */             !StringUtils.isEmpty(pair[1])) {
/* 390 */             charset = pair[1].trim();
/*     */           }
/*     */           
/*     */           break;
/*     */         } 
/*     */       } 
/*     */     } 
/*     */     
/* 398 */     return charset;
/*     */   }
/*     */   
/*     */   private static Map<String, String> getParamsFromUrl(String url) {
/* 402 */     Map<String, String> map = null;
/* 403 */     if (url != null && url.indexOf('?') != -1) {
/* 404 */       map = splitUrlQuery(url.substring(url.indexOf('?') + 1));
/*     */     }
/* 406 */     if (map == null) {
/* 407 */       map = new HashMap<String, String>();
/*     */     }
/* 409 */     return map;
/*     */   }
/*     */   
/*     */   private static URL buildGetUrl(String strUrl, String query) throws IOException {
/* 413 */     URL url = new URL(strUrl);
/* 414 */     if (StringUtils.isEmpty(query)) {
/* 415 */       return url;
/*     */     }
/*     */     
/* 418 */     if (StringUtils.isEmpty(url.getQuery())) {
/* 419 */       if (strUrl.endsWith("?")) {
/* 420 */         strUrl = strUrl + query;
/*     */       } else {
/* 422 */         strUrl = strUrl + "?" + query;
/*     */       }
/*     */     
/* 425 */     } else if (strUrl.endsWith("&")) {
/* 426 */       strUrl = strUrl + query;
/*     */     } else {
/* 428 */       strUrl = strUrl + "&" + query;
/*     */     } 
/*     */ 
/*     */     
/* 432 */     return new URL(strUrl);
/*     */   }
/*     */   
/*     */   public static String buildQuery(Map<String, String> params, String charset) throws IOException {
/* 436 */     if (params == null || params.isEmpty()) {
/* 437 */       return null;
/*     */     }
/*     */     
/* 440 */     StringBuilder query = new StringBuilder();
/* 441 */     Set<Map.Entry<String, String>> entries = params.entrySet();
/* 442 */     boolean hasParam = false;
/*     */     
/* 444 */     for (Map.Entry<String, String> entry : entries) {
/* 445 */       String name = entry.getKey();
/* 446 */       String value = entry.getValue();
/*     */       
/* 448 */       if (StringUtils.areNotEmpty(new String[] { name, value })) {
/* 449 */         if (hasParam) {
/* 450 */           query.append("&");
/*     */         } else {
/* 452 */           hasParam = true;
/*     */         } 
/*     */         
/* 455 */         query.append(name).append("=").append(URLEncoder.encode(value, charset));
/*     */       } 
/*     */     } 
/*     */     
/* 459 */     return query.toString();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static Map<String, String> splitUrlQuery(String query) {
/* 468 */     Map<String, String> result = new HashMap<String, String>();
/*     */     
/* 470 */     String[] pairs = query.split("&");
/* 471 */     if (pairs != null && pairs.length > 0) {
/* 472 */       for (String pair : pairs) {
/* 473 */         String[] param = pair.split("=", 2);
/* 474 */         if (param != null && param.length == 2) {
/* 475 */           result.put(param[0], param[1]);
/*     */         }
/*     */       } 
/*     */     }
/*     */     
/* 480 */     return result;
/*     */   }
/*     */   
/*     */   public <T extends AlipayResponse> T pageExecute(AlipayRequest<T> request) throws AlipayApiException {
/* 484 */     return null;
/*     */   }
/*     */ 
/*     */   
/*     */   public <T extends AlipayResponse> T pageExecute(AlipayRequest<T> request, String method) throws AlipayApiException {
/* 489 */     return null;
/*     */   }
/*     */ 
/*     */   
/*     */   public <TR extends AlipayResponse, T extends AlipayRequest<TR>> TR parseAppSyncResult(Map<String, String> result, Class<T> requsetClazz) throws AlipayApiException {
/* 494 */     return null;
/*     */   }
/*     */   
/*     */   public <T extends AlipayResponse> T sdkExecute(AlipayRequest<T> request) throws AlipayApiException {
/* 498 */     return null;
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Downloads\jd-gui-windows-1.6.6\jd-gui-windows-1.6.6\alipay-sdk-1.1.jar!\com\alipay\api\AlipayMobilePublicMultiMediaClient.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       1.1.3
 */